<?php
namespace Admin\Controller;
use Common\Controller\BasicController;
/**
 * 文章控制器
 */
class NewsController extends BasicController
{

  public function index(){
    $map = array();
    $info = I('get.');
    if(isset($info['kw'])){
        $map['keyword'] = $info['kw'];
    }
    $page = $_REQUEST['p']?$_REQUEST['p']:1;
    $pageSize = $_REQUEST['pageSize']?$_REQUEST['pageSize']:10;
    $count = D('News')->getNewsCount($map);
    $newsList = D('News')->getNewsList($page,$pageSize,$map);
    foreach ($newsList as $k => $v) {
      $newsList[$k]['newsType'] = D('NewsType')->getNewsType($v['type_id']);
    }
    $res = new \Think\Page($count,$pageSize);
    $pageRes = $res->show();
    $this->assign('pageRes',$pageRes);
    $this->assign('list',$newsList);
    $this->display();
  }
  public function add(){
    if(IS_POST){
      $info = I('post.');
      if(!trim($info['title'])){
        ajaxReturn(400,"文章名必须");
      }
      if(!trim($info['type_id'])){
        ajaxReturn(400,"文章分类必须");
      }
      if(trim($info['news_id'])){
        return $this->save($info);
      }
      $news['content'] = $info['content'];
      foreach ($info['tab'] as $k => $v) {
        $info['tab'][$k] = trim($v);
      }
      $info['tab'] = implode(',',$info['tab']);
      unset($info['content']);
      $res = D('News')->addNews($info);
      if($res){
        $news['news_id'] = $res;
        $result = D("News")->addNewContent($news);
        if($result){
          return ajaxReturn(200,"新增文章成功");
        }else{
          return ajaxReturn(400,"新增文章成功，内容插入失败");
        }
      }else{
          return ajaxReturn(400,"新增文章失败");
      }
    }
    $user = getLoginUserInfo();
    $newsType  = D('NewsType')->getNewsTypeById($user['user_id']);
    $this->assign('newsType',$newsType);
    $this->display();
  }
  public function setStatus(){
    if(IS_POST){
      $data = I('post.');
      $id = $data['id'];
      unset($data['id']);
      $status = $data['status'];
      try{
        $res = D('News')->updateNews($id,$data);
        if($res === false){                 //tp save方法失败返回false，只有false的时候才是失败。
          return ajaxReturn(400,'修改失败');
        }else{
          return ajaxReturn(200,'修改成功');
        }
      }catch(Exception $e){
        return ajaxReturn(400,$e->getMessage());
      }
    }
  }
  public function edit(){
    $id = I('get.id');
    if(!$id){
      $this->error("id不存在");
    }
    $news = D('News')->getNewsById($id);
    if($news){
      $content = D('News')->getNewsContent($news['news_id']);
      $news['content'] = htmlspecialchars_decode($content['content']);
    }
    $news['tab'] = explode(',',$news['tab']);
    if(!$news){
      $this->error("id不存在");
    }
    $userLevel = getLoginUserInfo();
    $cid  = D('Menu')->getNewsCid();
    $this->assign('cid',$cid);
    $this->assign('user',$user['level']);
    $this->assign('vo',$news);
    $this->display();
  }
  public function check(){
    if(IS_POST){
      $info = I('post.');
      if($info['news_id']){
        return $this->save($info);
      }else{
        return ajaxReturn(400,'文章ID不存在');
      }
    }
    $res = $this->rule('Admin/News/check');
    $id = I('get.id');
    if(!$id){
      $this->error("id不存在");
    }
    $news = D('News')->getNewsById($id);
    if($news){
      $content = D('News')->getNewsContent($news['news_id']);
      $news['content'] = htmlspecialchars_decode($content['content']);
    }
    $news['tab'] = explode(',',$news['tab']);
    if(!$news){
      $this->error("id不存在");
    }
    $user = getLoginUserInfo();
    $newsType = D('NewsType')->getNewsTypeById($user['user_id']);
    $this->assign('newsType',$newsType);
    $this->assign('vo',$news);
    $this->display();
  }
  public function save($data){
    $id = $data['news_id'];
    unset($data['news_id']);
    try{
      $res = D('News')->updateNews($id,$data);
      if($res === false){                 //tp save方法失败返回false，只有false的时候才是失败。
        return ajaxReturn(400,'修改失败');
      }else{
        return ajaxReturn(200,'修改成功');
      }
    }catch(Exception $e){
        return ajaxReturn(400,$e->getMessage());
    }
  }
}
